﻿using System;
using System.Linq;
using NDBGen.Descriptors.Columns.Tokens;
using NDBGen.Plugins.DataAccess;

namespace NDBGen.Plugins.Descriptors.Columns.Tokens
{
    [ColumnTokenCodeSnippetGenerator(typeof(ScriptedDataMetadataDescriptor))]
    public sealed class DotNetScriptedDataMetadataCodeGenerator : DotNetColumnTokenCodeGeneratorBase<ScriptedDataMetadataDescriptor>
    {
        protected override Object[] GenerateCustomAttributes()
        {
            var lookupColumns = (
                from schema in this.ParsedInput.ScriptedData
                from table in schema
                from column in table.Columns
                where table.TableKey == this.Descriptor.Table.Key
                    && column.IsLookupKey
                    && column.Name == this.Descriptor.Column.Name
                select column);

            if (lookupColumns.Any())
            {
                return new Object[] { ColumnMetadataHelper.CustomAttributes.LookupKeyMember };
            }
            else return ColumnMetadataHelper.NoCustomAttributes;
        }

        public override String ToString()
        {
            return null;
        }
    }
}